//
//  BZ_PasswordView.m
//  penzer
//
//  Created by caiyc on 2018/11/21.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "BZ_PasswordView.h"
@interface BZ_PasswordView()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *pswTF;
@property (nonatomic,strong) NSMutableArray<UILabel *> *items;

@end
@implementation BZ_PasswordView

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder]){
        [self initUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]){
        [self initUI];
    }
    return self;
}


#pragma mark - 设置UI
- (void)initUI
{
    self.backgroundColor = [UIColor whiteColor];
    _items = [NSMutableArray array];
    [self addSubview:self.pswTF];
    
    //设置边框圆角与颜色
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 10.0;
    self.layer.borderColor = [UIColor colorWithRed:63/255.0 green:42/255.0 blue:178/255.0 alpha:1].CGColor;
    self.layer.borderWidth = 1;
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
    [self addGestureRecognizer:tap];
    
    for (int i = 0; i < 6; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:40];
        label.textColor =[UIColor blackColor];
        [self addSubview:label];
        [self.items addObject:label];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.frame.size.width/self.items.count;
    [self.items enumerateObjectsUsingBlock:^(UILabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
        label.frame = CGRectMake(idx*width, 0, width, self.frame.size.height);
    }];
}


//划线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1);
    //设置分割线颜色
    CGContextSetRGBStrokeColor(context, 63/255.0, 42/255.0, 178/255.0, 1);
    CGContextBeginPath(context);
    
    for (int i = 0; i < 5;  i++){
        CGContextMoveToPoint(context, self.frame.size.width/6.0 * (i + 1), 0);
        CGContextAddLineToPoint(context,self.frame.size.width/6.0 * (i + 1) , self.frame.size.height);
    }
    CGContextStrokePath(context);
}



#pragma mark - 监视textField值
- (void)valueChange:(UITextField *)textField
{
    NSString *text = textField.text;
    if (text.length <= self.items.count){
        [self.items enumerateObjectsUsingBlock:^(UILabel * _Nonnull label, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx < text.length) {
                label.text = [text substringWithRange:NSMakeRange(idx, 1)];
            }else{
                label.text = @"";
            }
        }];
        
    }else{
        textField.text = [text substringWithRange:NSMakeRange(0, 6)];
    }
    
    NSLog(@"===%@====",textField.text);
}



#pragma mark - 点击view开始输入
- (void)click{
    [self.pswTF becomeFirstResponder];
}

#pragma mark - 关闭键盘
- (void)closeKeyborad{
    [self.pswTF resignFirstResponder];
}


#pragma mark - setter&&getter
- (UITextField *)pswTF
{
    if (!_pswTF) {
        _pswTF = [[UITextField alloc] init];
        _pswTF.delegate = self;
        _pswTF.keyboardType = UIKeyboardTypeNumberPad;
        [_pswTF becomeFirstResponder];
        //添加对输入值的监视
        [_pswTF addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _pswTF;
}
@end
